package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.party.WorkInfo;
import com.tongzhan.entity.party.WorkSchedule;
import com.tongzhan.param.WorkParam;
import com.tongzhan.utils.DateSwitcher;
import com.tongzhan.utils.SnowFlakeUtils;
import com.tongzhan.vo.WorkInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作安排
 *
 * @author htmic
 * @date 2020/7/29
 */
@RequestMapping("/work")
@RestController
public class WorkController extends BaseController {

    /**
     * 工作安排列表
     * @param page 页码
     * @param size 每页个数
     * @param partyTypeId 党派id
     * @return
     */
    @PostMapping("/workList")
    public Return workList(Integer partyTypeId, Integer page, Integer size) {
        page = page == null ? 0 : page - 1;
        Page<WorkInfo> list = workService.workList(partyTypeId, page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list.getContent());
        map.put("total", list.getTotalElements());
        map.put("totalpage", list.getTotalPages());
        return Return.getSuccessResult(map);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("/workDetail")
    public Return getDetail(String id,String userId) {
        if (id == null) {
            return Return.getErrorResult("缺少id参数!");
        }
        WorkInfoVo vo = workService.workDetail(id,userId);
        return Return.getSuccessResult(vo);
    }

    /**
     * 新增工作安排
     * @param param
     * @return
     */
    @PostMapping("/addSchedule")
    public Return addSchedule(@RequestBody WorkParam param) {
        if (param.getPartyTypeId()==null) {
            return Return.getErrorResult("党派类型不能为空");
        }
        WorkInfo work = new WorkInfo();
        BeanUtils.copyProperties(param, work);
        SnowFlakeUtils snowFlake = new SnowFlakeUtils(3, 3);
        work.setId(String.valueOf(snowFlake.nextId()));
        work.setAddUserId(param.getUserId());
        work.setTitle(DateSwitcher.date2String(work.getTime(),"yyy-MM") +"工作安排");//标题(${年月}+${工作安排})

        List<WorkSchedule> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(param.getList())) {
            param.getList().forEach(item->{
                WorkSchedule workSchedule = new WorkSchedule();
                SnowFlakeUtils snowFlake2 = new SnowFlakeUtils(3, 3);
                work.setId(String.valueOf(snowFlake2.nextId()));
                BeanUtils.copyProperties(item, workSchedule);
                list.add(workSchedule);
            });
        }
        long l = workService.addSchedule(work, list);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }

}
